﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ChinaDynastyTours.Models;
using ChinaDynastyTours.Controls;
using ChinaDynastyTours.Views.FinanceGroup;
using System.Threading;
using ChinaDynastyTours.Helpers;
using DevExpress.XtraGrid.Views.Grid;

namespace ChinaDynastyTours.Views
{
    public partial class AccountManagerForm : DevExpress.XtraEditors.XtraForm
    {
        private DataTable _dtGuestListData;
        public AccountManagerForm()
        {
            InitializeComponent();
        }

        private void AccountManagerForm_Load(object sender, EventArgs e)
        {
            dateStart.EditValue = DateTime.Parse(string.Format("{0}-{1}-{2}", DateTime.Now.Year, DateTime.Now.Month, "01"));
            dateEnd.EditValue = DateTime.Parse(string.Format("{0}-{1}-{2}", DateTime.Now.Year, DateTime.Now.Month, DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month)));

            if (ApplicationHelper.RankID == 1 || ApplicationHelper.RankID == 0)//只有级别为0或1的才允许编辑
                accountManageGridView.Columns["AuditState"].OptionsColumn.AllowEdit = true;

            btnRefresh.PerformClick();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            string currentUser = UserInfoCtrl.GetNameByUserName(Thread.CurrentPrincipal.Identity.Name);//当前登录者
            decimal beginPeroid, endPeroid;

            if (ApplicationHelper.RankID == 2)
            {
                _dtGuestListData = AccountManagerCtrl.GetAccountManagerData(dateStart.DateTime.ToString("yyyy-MM-dd"), dateEnd.DateTime.ToString("yyyy-MM-dd"), AccountType.Text.ToString(), currentUser);
                AccountManagerCtrl.GetAccountManagerPeriod(dateStart.DateTime.ToString("yyyy-MM-dd"), dateEnd.DateTime.ToString("yyyy-MM-dd"), AccountType.Text.ToString(), currentUser, out beginPeroid,out endPeroid);
            }
            else
            {
                _dtGuestListData = AccountManagerCtrl.GetAccountManagerData(dateStart.DateTime.ToString("yyyy-MM-dd"), dateEnd.DateTime.ToString("yyyy-MM-dd"), AccountType.Text.ToString(), "");
                AccountManagerCtrl.GetAccountManagerPeriod(dateStart.DateTime.ToString("yyyy-MM-dd"), dateEnd.DateTime.ToString("yyyy-MM-dd"), AccountType.Text.ToString(), currentUser, out beginPeroid, out endPeroid);
            }
            tbBeginPeriod.EditValue=beginPeroid;
            tbEndPeriod.EditValue = endPeroid;
            _dtGuestListData.RowChanged += new DataRowChangeEventHandler(Table_RowChanged);

            accountManagerGridControl.DataSource = _dtGuestListData;

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {

            AddAccountItemForm accountItemForm = new AddAccountItemForm();
            if (accountItemForm.ShowDialog(this) == DialogResult.OK)
            {
                string currentUser = UserInfoCtrl.GetNameByUserName(Thread.CurrentPrincipal.Identity.Name);//当前登录者
                
                #region 计入流水账
                AccountManagerEntity entity = new AccountManagerEntity();
                entity.Date = DateTime.Now.ToString("yyyy-MM-dd");
                entity.Summary = accountItemForm.tbRemark.Text;
                if (accountItemForm.ItemtypeGroup.SelectedIndex == 0)
                {
                    entity.Income = decimal.Parse(accountItemForm.tbAccount.EditValue.ToString());
                    entity.Expenditure = 0;
                }
                else
                {
                    entity.Income = 0;
                    entity.Expenditure = decimal.Parse(accountItemForm.tbAccount.EditValue.ToString());
                }
                entity.Remark = "";
                entity.ItemType = 3;
                entity.ItemID = "";
                entity.Voucher = accountItemForm.tbVoucher.Text;
                entity.PaymentMethod = accountItemForm.tbPaymentMethod.Text;
                switch (accountItemForm.tbPaymentMethod.Text)
                {
                    case "现金":
                        entity.AccountType = AccountTypeEnum.现金.ToString();
                        break;
                    case "贝宝":
                        entity.AccountType = AccountTypeEnum.贝宝.ToString();
                        break;
                    case "其他":
                        entity.AccountType = AccountTypeEnum.其他.ToString();
                        break;
                    default:
                        entity.AccountType = AccountTypeEnum.银行.ToString();
                        break;
                } 
                entity.Transactor = currentUser;

                AccountManagerCtrl.CreateAccountManagerData(entity);
                #endregion

                btnRefresh.PerformClick();//刷新
            }
        }
        private void btnVoucher_Click(object sender, EventArgs e)
        {
            if (accountManageGridView.FocusedRowHandle < 0) return;


            if (accountManageGridView.GetRowCellValue(accountManageGridView.FocusedRowHandle, "Voucher").ToString() != "")
            {
                MessageBox.Show("已有凭证号不能修改");
                return;
            }
            switch (accountManageGridView.GetRowCellValue(accountManageGridView.FocusedRowHandle, "ItemType").ToString())
            {
                case "0":
                    MessageBox.Show("付款单凭证号请在“付款单打印”界面修改此凭证号");
                    return;
                case "1":
                    MessageBox.Show("报销凭证号请在“报销付款”界面修改此凭证号");
                    return;
                case "2":
                    MessageBox.Show("收款凭证号请在“收款录入”界面修改此凭证号");
                    return;
                default:
                    break;
            }


            VoucherModifyForm voucherForm = new VoucherModifyForm();
            if (voucherForm.ShowDialog(this) == DialogResult.OK)
            {
                string accountManagerID = accountManageGridView.GetRowCellValue(accountManageGridView.FocusedRowHandle, "AccountManagerID").ToString();

                //刷新界面
                accountManageGridView.SetRowCellValue(accountManageGridView.FocusedRowHandle, "Voucher", voucherForm.tbVoucher.Text);

                AccountManagerCtrl.UpdateVoucherDataByID(accountManagerID, voucherForm.tbVoucher.Text);
            }
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (accountManageGridView.FocusedRowHandle < 0) return;

            if (MessageBox.Show("确定删除当前选择的信息吗？", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                string AccountManagerID = accountManageGridView.GetRowCellValue(accountManageGridView.FocusedRowHandle, "AccountManagerID").ToString();
                accountManageGridView.DeleteRow(accountManageGridView.FocusedRowHandle);
                AccountManagerCtrl.DeleteAccountManagerData(AccountManagerID);
            }
        }
        void Table_RowChanged(object sender, DataRowChangeEventArgs e)
        {
            if (e.Action == DataRowAction.Change || e.Action == DataRowAction.ChangeCurrentAndOriginal || e.Action == DataRowAction.ChangeOriginal)
            {
                string AccountManagerID = accountManageGridView.GetRowCellValue(accountManageGridView.FocusedRowHandle, "AccountManagerID").ToString();

                AccountManagerEntity entity = new AccountManagerEntity()
                {
                    AccountManagerID = int.Parse(accountManageGridView.GetRowCellValue(accountManageGridView.FocusedRowHandle, "AccountManagerID").ToString()),
                    AuditState=  bool.Parse(e.Row["AuditState"].ToString()) == true ? 1 : 0,
                    Date = e.Row["Date"].ToString(),
                    Expenditure = decimal.Parse(e.Row["Expenditure"].ToString()),
                    Income = decimal.Parse(e.Row["Income"].ToString()),
                    Summary = e.Row["Summary"].ToString(),
                    Remark = e.Row["Remark"].ToString(),
                };
                AccountManagerCtrl.UpdateAccountManagerData(entity);
            }
        }
        private void accountManageGridView_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            AccountManagerEntity entity = new AccountManagerEntity();
            entity.AuditState = 0;
            entity.Date = DateTime.Now.ToString("yyyy-MM-dd");
            entity.Summary = "";
            entity.Income = 0;
            entity.Expenditure = 0;
            entity.Remark = "";

            string AccountManagerID = AccountManagerCtrl.CreateAccountManagerData(entity);

            accountManageGridView.SetRowCellValue(e.RowHandle, "AccountManagerID", AccountManagerID);
            accountManageGridView.SetRowCellValue(e.RowHandle, "Date", entity.Date);
            accountManageGridView.SetRowCellValue(e.RowHandle, "Summary", entity.Summary);
            accountManageGridView.SetRowCellValue(e.RowHandle, "Income", entity.Income);
            accountManageGridView.SetRowCellValue(e.RowHandle, "Expenditure", entity.Expenditure);
            accountManageGridView.SetRowCellValue(e.RowHandle, "Remark", entity.Remark);
        }
        private void AccountManageForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            //激发行程计划的GrideView数据更新
            if (accountManageGridView.FocusedRowModified)
                accountManageGridView.FocusedRowHandle = accountManageGridView.FocusedRowHandle == 0 ? 1 : 0;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void accountManageGridView_RowStyle(object sender, RowStyleEventArgs e)
        {
            if (e.RowHandle < 0) return;
            object selected = accountManageGridView.GetRowCellValue(e.RowHandle, "AuditState");

            if (string.IsNullOrEmpty(selected.ToString())) return;

            if (Convert.ToBoolean(selected) == true)
            {
                e.Appearance.BackColor = Color.Wheat;
                e.Appearance.BackColor2 = Color.Azure;
            }
            else
            {
                e.Appearance.BackColor = Color.Empty;
                e.Appearance.BackColor2 = Color.Empty;
            }
        }


    }
}
